<?php
/**
 * Created by IntelliJ IDEA.
 * User: luwei
 * Date: 2017/7/25
 * Time: 15:44
 */

namespace app\modules\api\models;

use app\hejiang\ApiCode;
use app\models\PurchaseApply;
use app\models\Model;

class PurchaseSaveForm extends ApiModel
{
     public $store_id;
    public $model;
	public $status;
	public $user_id;
    public $realname;
    public $tel;
    public $district;
    public $name;
    public $address;

    public $review_result;


    public function rules()
    {
        return [
            [['store_id','user_id'], 'integer'],
			[['realname', 'tel', 'name', 'address'], 'required'],
            [['realname', 'tel', 'name', 'district','address'], 'string','max'=>250],
            [['user_id'], 'integer']
        ];
    }

    public function attributeLabels() 
    { 
        return [ 
            'store_id' => 'Store ID',
            'user_id' => '用户',
			'name'=>'团队名称',
			'realname' => '联系人',
            'tel' => '联系电话',
            'address' => '详细地址',
			'district'=>'简介',
           
        ]; 
    }

    public function save()
    {
		$address = new PurchaseApply();
        if (!$this->validate()) {
            return $this->errorResponse;
        }

        $address->store_id = $this->store_id;
		$model = PurchaseApply::findOne([
						'user_id' => $this->user_id,
						'store_id' => $this->store->id,
					]);
					
		if($model){
			return [
                'code' => 0,
                'msg'  => '你已经申请过了，请不要重复申请',
            ];
        }	
        $address->user_id = $this->user_id;
       
        $address->create_time = time();
        $address->name = $this->name;
        $address->realname = $this->realname;
        $address->tel = $this->tel;
        $address->address = $this->address;
        $address->district = $this->district;

        if ($address->save()) {
            return [
                'code' => ApiCode::CODE_SUCCESS,
                'msg'  => '保存成功',
            ];
        } else {
            return [
                'code' => 0,
                'msg'  => '操作失败，请稍后重试',
            ];
        }
    }
}
